
// https://leetcode.cn/problems/binary-search/submissions/
/**
 * 给定一个 n 个元素有序的（升序）整型数组 nums 和一个目标值 target  ，
 * 写一个函数搜索 nums 中的 target，如果目标值存在返回下标，否则返回 -1。
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    let left=0;
    let right=nums.length-1;
    let offset=0;
    let mid=0;
    while(left<=right){
        mid=Math.floor((right-left)/2)+offset
        console.log(left,right,mid,offset)
         if(nums[mid]===target){
              return mid
          }else if(nums[mid]>target){
              right=mid-1
              offset=right;
          }else{
              left=mid+1
              offset=left;
          }

    }
 return -1;
 
};


// 不需要offset的写法
var search = function(nums, target) {
    let left=0;
    let right=nums.length-1;
    
    let mid=0;
    while(left<=right){
        mid=Math.floor((right+left)/2)
        console.log(left,right,mid,offset)
         if(nums[mid]===target){
              return mid
          }else if(nums[mid]>target){
              right=mid-1
          }else{
              left=mid+1
          }

    }
 return -1;
 
};


console.log(search([-1,0,3,5,9,12],-1))